<?php
/*
本系统由苏酷伊基于SiteHub项目二次开发
作者博客： https://www.sukuy.com/
后续更新地址： https://www.sukuy.com/navigation
作者QQ：3288637559
QQ交流群：884250547
任何第三方非官网在发布本系统时，不得删除该注释、版权信息、作者信息及官网等内容
*/
require('../includes/common.php');
require('../includes/lang.class.php');
$title = '异常站点管理';
require('./header.php');
require('./navbar.php');
require('./sidebar.php');

$page = _get('page', 1);
$status = _get('status', -1);
$pagesize = 20;
$offset = ($page - 1) * $pagesize;


if($_GET['act'] == 'delete') {
    $id = intval($_GET['id']);
    $DB->delete('52dh_site_feedback', array('id' => $id));
    
    
    $log = array(
        'type' => 'action',
        'username' => $conf['admin_user'],
        'action' => '删除异常站点反馈，ID: ' . $id,
        'ip' => get_ip(),
        'user_agent' => $_SERVER['HTTP_USER_AGENT'],
        'result' => 1
    );
    $DB->insert('log', $log);
    
    showmsg('删除成功', $_SERVER['HTTP_REFERER']);
} elseif($_GET['act'] == 'process') {
    $id = intval($_GET['id']);
    $DB->update('52dh_site_feedback', array('status' => 1), array('id' => $id));
    
    
    $log = array(
        'type' => 'action',
        'username' => $conf['admin_user'],
        'action' => '处理异常站点反馈，ID: ' . $id,
        'ip' => get_ip(),
        'user_agent' => $_SERVER['HTTP_USER_AGENT'],
        'result' => 1
    );
    $DB->insert('log', $log);
    
    showmsg('标记为已处理成功', $_SERVER['HTTP_REFERER']);
} elseif($_GET['act'] == 'batch') {
    if($_POST['ids']) {
        $ids = explode(',', $_POST['ids']);
        foreach($ids as $id) {
            $id = intval($id);
            if($id > 0) {
                if($_POST['batch_action'] == 'delete') {
                    $DB->delete('52dh_site_feedback', array('id' => $id));
                } elseif($_POST['batch_action'] == 'process') {
                    $DB->update('52dh_site_feedback', array('status' => 1), array('id' => $id));
                }
            }
        }
        
        
        $log = array(
            'type' => 'action',
            'username' => $conf['admin_user'],
            'action' => '批量' . ($_POST['batch_action'] == 'delete' ? '删除' : '处理') . '异常站点反馈，ID: ' . $_POST['ids'],
            'ip' => get_ip(),
            'user_agent' => $_SERVER['HTTP_USER_AGENT'],
            'result' => 1
        );
        $DB->insert('log', $log);
    }
    showmsg('操作成功', $_SERVER['HTTP_REFERER']);
}


$where = array();
if($status != -1) {
    $where['status'] = $status;
}


$keyword = isset($_GET['keyword']) ? daddslashes($_GET['keyword']) : '';
if($keyword) {
    $where[] = ' (site_name LIKE :keyword OR site_url LIKE :keyword OR feedback_desc LIKE :keyword) ';
    $bindParams[':keyword'] = '%' . $keyword . '%';
}


$total = $DB->count('52dh_site_feedback', $where);


$pages = ceil($total / $pagesize);
$page = min($pages, max($page, 1));
$offset = ($page - 1) * $pagesize;


$feedbacks = $DB->findAll('52dh_site_feedback', '*', $where, 'create_time DESC', array($offset, $pagesize));


$pagelink = pagination($total, $pagesize, $page, 'site_feedback.php?status=' . $status . '&keyword=' . urlencode($keyword) . '&page=');

function getStatusText($status) {
    return $status == 0 ? '<span class="label label-warning">待处理</span>' : '<span class="label label-success">已处理</span>';
}
?>
<div class="content-wrapper">
    <section class="content-header"><ol class="breadcrumb">
            <li><a href="./"><?php echo $lang->admin->index; ?></a></li>
            <li class="active"><?php echo $title; ?></li>
        </ol>
    </section>
    <section class="content">
        <div class="panel panel-default">
            <div class="panel-heading"><h3 class="panel-title"><i class="fa fa-exclamation-triangle fa-fw"></i> <?php echo $title; ?></h3></div>
            <div class="panel-body">
                <form action="site_feedback.php" method="get" class="form-inline" role="form">
                    <div class="form-group">
                        <select name="status" class="form-control">
                            <option value="-1" <?php if($status == -1) echo 'selected'; ?>>全部状态</option>
                            <option value="0" <?php if($status == 0) echo 'selected'; ?>>待处理</option>
                            <option value="1" <?php if($status == 1) echo 'selected'; ?>>已处理</option>
                        </select>
                    </div>
                    <div class="form-group">
                        <div class="input-group">
                            <input type="text" class="form-control" name="keyword" placeholder="搜索站点名称/URL/反馈描述" value="<?php echo $keyword; ?>">
                            <span class="input-group-btn">
                                <button type="submit" class="btn btn-primary"><i class="fa fa-search fa-fw"></i> 搜索</button>
                            </span>
                        </div>
                    </div>
                </form>
            </div>
        </div>
        
        <form id="batchForm" action="site_feedback.php?act=batch" method="post" class="form-inline mb-10">
            <div class="form-group">
                <select name="batch_action" class="form-control">
                    <option value="">批量操作</option>
                    <option value="process">标记为已处理</option>
                    <option value="delete">删除所选</option>
                </select>
            </div>
            <button type="submit" class="btn btn-primary ml-10" onclick="return confirm('确定要执行该批量操作吗？');">执行</button>
            <input type="hidden" name="ids" id="batchIds" value="">
        </form>
        
        <div class="table-responsive">
            <table class="table table-striped">
                <thead>
                    <tr>
                        <th><input type="checkbox" id="selectAll" onclick="selectAll(this)"></th>
                        <th>ID</th>
                        <th>站点名称</th>
                        <th>反馈类型</th>
                        <th>反馈描述</th>
                        <th>提交时间</th>
                        <th>状态</th>
                        <th>操作</th>
                    </tr>
                </thead>
                <tbody>
                    <?php if($feedbacks) foreach($feedbacks as $feedback): ?>
                    <tr>
                        <td><input type="checkbox" class="itemCheckbox" value="<?php echo $feedback['id']; ?>"></td>
                        <td><?php echo $feedback['id']; ?></td>
                        <td><a href="/site.php?id=<?php echo $feedback['site_id']; ?>" target="_blank" title="<?php echo $feedback['site_url']; ?>"><?php echo $feedback['site_name']; ?></a></td>
                        <td><?php echo $feedback['feedback_type']; ?></td>
                        <td><div style="max-width:200px; word-break:break-all;" title="<?php echo $feedback['feedback_desc']; ?>"><?php echo mb_substr($feedback['feedback_desc'], 0, 50, 'utf-8'); ?><?php if(mb_strlen($feedback['feedback_desc'], 'utf-8') > 50) echo '...'; ?></div></td>
                        <td><?php echo $feedback['create_time']; ?></td>
                        <td><?php echo getStatusText($feedback['status']); ?></td>
                        <td>
                            <a href="#" class="btn btn-xs btn-info" onclick="showDetail('<?php echo $feedback['id']; ?>');">详情</a>
                            <?php if($feedback['status'] == 0): ?>
                            <a href="site_feedback.php?act=process&id=<?php echo $feedback['id']; ?>&page=<?php echo $page; ?>&status=<?php echo $status; ?>&keyword=<?php echo urlencode($keyword); ?>" class="btn btn-xs btn-success" onclick="return confirm('确定要标记为已处理吗？');">处理</a>
                            <?php endif; ?>
                            <a href="site_feedback.php?act=delete&id=<?php echo $feedback['id']; ?>&page=<?php echo $page; ?>&status=<?php echo $status; ?>&keyword=<?php echo urlencode($keyword); ?>" class="btn btn-xs btn-danger" onclick="return confirm('确定要删除该反馈吗？');">删除</a>
                        </td>
                    </tr>
                    <?php endforeach; ?>
                    <?php if(!$feedbacks): ?>
                    <tr><td colspan="8" class="text-center">暂无异常反馈记录</td></tr>
                    <?php endif; ?>
                </tbody>
            </table>
        </div>
        
        <?php if($pages > 1): ?>
        <nav class="text-center">
            <ul class="pagination">
                <?php echo $pagelink; ?>
            </ul>
        </nav>
        <?php endif; ?>
    </section>
</div>

<!-- 详情模态框 -->
<div id="detailModal" class="modal fade" tabindex="-1" role="dialog" aria-labelledby="detailModalLabel">
    <div class="modal-dialog modal-lg" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                <h4 class="modal-title" id="detailModalLabel">反馈详情</h4>
            </div>
            <div class="modal-body" id="detailContent">
                <!-- 加载中 -->
                <div class="text-center py-30">
                    <i class="fa fa-spinner fa-spin fa-3x text-primary"></i>
                    <p class="mt-10">加载中...</p>
                </div>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
            </div>
        </div>
    </div>
</div>

<script>
// 全选/取消全选
function selectAll(checkbox) {
    $('.itemCheckbox').prop('checked', checkbox.checked);
    updateBatchIds();
}

// 更新批量操作的ID列表
function updateBatchIds() {
    var ids = [];
    $('.itemCheckbox:checked').each(function() {
        ids.push($(this).val());
    });
    $('#batchIds').val(ids.join(','));
}

// 单个复选框点击事件
$('.itemCheckbox').click(function() {
    updateBatchIds();
    // 检查是否所有都选中了
    var allChecked = true;
    $('.itemCheckbox').each(function() {
        if(!$(this).prop('checked')) {
            allChecked = false;
            return false;
        }
    });
    $('#selectAll').prop('checked', allChecked);
});

// 显示详情
function showDetail(id) {
    // 显示加载中
    $('#detailContent').html('<div class="text-center py-30"><i class="fa fa-spinner fa-spin fa-3x text-primary"></i><p class="mt-10">加载中...</p></div>');
    
    // AJAX请求详情
    $.ajax({
        url: '../api/feedback.php',
        type: 'GET',
        data: { 'act': 'detail', 'id': id },
        dataType: 'json',
        success: function(response) {
            if(response.code == 0) {
                var feedback = response.data;
                var html = '<div class="row">'
                    + '<div class="col-md-6">'
                    + '<table class="table table-borderless">'
                    + '<tr><td width="100">ID：</td><td>' + feedback.id + '</td></tr>'
                    + '<tr><td>站点名称：</td><td><a href="/site.php?id=' + feedback.site_id + '" target="_blank">' + feedback.site_name + '</a></td></tr>'
                    + '<tr><td>站点URL：</td><td><a href="' + feedback.site_url + '" target="_blank">' + feedback.site_url + '</a></td></tr>'
                    + '<tr><td>反馈类型：</td><td>' + feedback.feedback_type + '</td></tr>'
                    + '<tr><td>提交时间：</td><td>' + feedback.create_time + '</td></tr>'
                    + '<tr><td>状态：</td><td>' + (feedback.status == 0 ? '<span class="label label-warning">待处理</span>' : '<span class="label label-success">已处理</span>') + '</td></tr>'
                    + '<tr><td>提交IP：</td><td>' + feedback.ip + '</td></tr>'
                    + (feedback.contact_info ? '<tr><td>联系方式：</td><td>' + feedback.contact_info + '</td></tr>' : '')
                    + '</table>'
                    + '</div>'
                    + '<div class="col-md-6">'
                    + '<h4>反馈详细描述</h4>'
                    + '<div class="bg-gray p-10" style="white-space: pre-wrap; word-break: break-all;">' + feedback.feedback_desc + '</div>'
                    + '</div>'
                    + '</div>';
                $('#detailContent').html(html);
            } else {
                $('#detailContent').html('<div class="alert alert-danger text-center">' + response.msg + '</div>');
            }
        },
        error: function() {
            $('#detailContent').html('<div class="alert alert-danger text-center">加载失败，请稍后重试</div>');
        }
    });
    
    // 显示模态框
    $('#detailModal').modal('show');
}

// 批量操作表单提交前检查
$('#batchForm').submit(function() {
    if(!$('select[name="batch_action"]').val()) {
        alert('请选择批量操作类型');
        return false;
    }
    if(!$('#batchIds').val()) {
        alert('请选择要操作的记录');
        return false;
    }
    return true;
});
</script>

<?php require('./footer.php'); ?>
</body>
</html>